#[cfg(test)]
mod test {
    #[test]
    fn iterator_demonstration() {
        let v1 = vec![1, 2, 3];
        // for不用mut是因为for已经取得了所有权
        let mut v1_iter = v1.iter();
        assert_eq!(v1_iter.next(), Some(&1));
        assert_eq!(v1_iter.next(), Some(&2));
        assert_eq!(v1_iter.next(), Some(&3));
    }

    // 迭代器
    #[derive(PartialEq, Debug)]
    struct Shoe {
        size: i32,
        style: String,
    }

    fn shoes_in_my_size(shoes: Vec<Shoe>, my_size: i32) -> Vec<Shoe> {
        shoes
            .into_iter()
            .filter(|item| item.size == my_size)
            .collect()
    }

    #[test]
    fn test_shoe() {
        let shoes = vec![
            Shoe {
                size: 8,
                style: String::from("888888"),
            },
            Shoe {
                size: 9,
                style: String::from("99999"),
            },
            Shoe {
                size: 12,
                style: String::from("121212"),
            },
            Shoe {
                size: 8,
                style: String::from("8888"),
            },
        ];
        assert_eq!(
            shoes_in_my_size(shoes, 8),
            vec![
                Shoe {
                    size: 8,
                    style: String::from("888888")
                },
                Shoe {
                    size: 8,
                    style: String::from("8888")
                }
            ]
        );
    }

    // 自建迭代器
    struct Counter {
        count: u32,
    }

    impl Counter {
        fn new() -> Counter {
            Counter { count: 0 }
        }
    }
    impl Iterator for Counter {
        type Item = u32;
        fn next(&mut self) -> Option<Self::Item> {
            if self.count < 5 {
                self.count += 1;
                Some(self.count)
            } else {
                None
            }
        }
    }

    #[test]
    fn using_other_oterator_trait_methods() {
        let c: u32 = Counter::new()
            .zip(Counter::new().skip(1)) // 类似拉链 将两个迭代器捏到一起  skip()跳过一项
            .map(|(a, b)| a * b)
            .filter(|x| x % 3 == 0)
            .sum();
        assert_eq!(18, c);
    }

    #[test]
    fn calling_next_directly() {
        let mut c = Counter::new();
        assert_eq!(c.next(), Some(1));
        assert_eq!(c.next(), Some(2));
        assert_eq!(c.next(), Some(3));
        assert_eq!(c.next(), Some(4));
        assert_eq!(c.next(), Some(5));
        assert_eq!(c.next(), Some(6));
        assert_eq!(c.next(), None);
    }

}
